<?php 
    class Habit_content_model extends CI_Model{

        function __construct(){
            parent::__construct();
        }

        function get_list($limit = 0, $offset = 0, $condtions = array()){
            $where = '1=1';
            if(!empty($condtions['name'])){
                $where .= " AND name like '%".$data['name']."%'";
            }
            $sql  = "SELECT SQL_CALC_FOUND_ROWS h.*,a.id as articles_id,a.title,t.name as habit_name,t.`image` as habit_img from  {$this->db->dbprefix}habit_content h  
                left join {$this->db->dbprefix}articles a on a.id = h.content_id
                left join {$this->db->dbprefix}task_types t on t.id = h.habit_id
                WHERE $where ORDER BY h.id desc ";

            
            if($limit > 0 || $offset > 0){
                $sql .= " LIMIT $limit, $offset";
            }
            $result = $this->db->query($sql)->result_array();
            if($limit > 0 || $offset > 0){
                $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
                return array("count" => $row_num['row_num'], "result" => $result);
            }else{
                return $result;
            }
        }




    }